package gui_layer;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxEditor;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
/**
 * This class is an expansion and customization of a sourcecode located at 
 * http://coddicted.com/java-custom-jcombobox-with-autocompletion/
 * We claim no rights to that code, even though that it was actually posted as a free for use software without any licence.
 * 
 */
@SuppressWarnings("serial")
public class DynamicSearchComboBoxModel extends AbstractListModel
                implements ComboBoxModel, KeyListener, ItemListener
{
    private List<String> all_data = new ArrayList<String>();
    private List<String> matching_data = new ArrayList<String>();
    private String selection;
    private JComboBox<String> combo_box;
    private ComboBoxEditor editor;
    private int current_pos = 0;
    private boolean handled;

    public DynamicSearchComboBoxModel(JComboBox<String> jcb)
    {
    	handled = false;
        combo_box = jcb;
        editor = jcb.getEditor();
        editor.getEditorComponent().addKeyListener(this);

    }
    
    public void start_anew()
    {
    	selection = null;
    	matching_data.clear();
    	current_pos = 0;
    	editor.setItem(null);
    	combo_box.setSelectedIndex(-1);

    }
    
    public boolean is_handled()
    {
    	return handled;
    }
    
    public void set_handled(boolean h)
    {
    	handled = h;
    }
    
    public void set_all_data(List<String> new_data)
    {
    	all_data = new_data;
    }

    public void updateModel(String typed_string)
    {
    	matching_data.clear();
//lets find any items which start with the string the user typed, and add it to the popup list
//here you would usually get your items from a database, or some other storage...
    	selection = typed_string;
        for(String s : all_data)
            if(s.toLowerCase().startsWith(typed_string.toLowerCase()))
            	matching_data.add(s);

        super.fireContentsChanged(this, 0, matching_data.size());

//this is a hack to get around redraw problems when changing the list length of the displayed popups


    	combo_box.hidePopup();
        combo_box.showPopup();


        
        /*if(matching_data.size() != 0)
        	combo_box.setSelectedIndex(0);*/
    }

    public int getSize(){return matching_data.size();}
    public Object getElementAt(int index)
    {
    	return matching_data.get(index);
    }
    public void setSelectedItem(Object anItem)
    {
    	selection = (String) anItem;

    }
    public Object getSelectedItem()
    {
    	return selection;
    }
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e){}

    public void keyReleased(KeyEvent e)
    {
        String str = editor.getItem().toString();
        JTextField jtf = (JTextField)editor.getEditorComponent();
        current_pos = jtf.getCaretPosition();

        if(e.getKeyChar() == KeyEvent.CHAR_UNDEFINED)
        {
            if(e.getKeyCode() != KeyEvent.VK_ENTER )
            {
            	//item = str;
                editor.setItem(str);
                jtf.setCaretPosition(current_pos);
            }
        }
        else if(e.getKeyCode() == KeyEvent.VK_ENTER)
        {
        	System.out.println("The listener in model worked");
            combo_box.setSelectedIndex(combo_box.getSelectedIndex());
            combo_box.actionPerformed(new ActionEvent(this, 13, "enterPressed"));
        }
        else
        {
            updateModel(combo_box.getEditor().getItem().toString());
            editor.setItem(str);
            jtf.setCaretPosition(current_pos);
        }
    }
    
    
    

    public void itemStateChanged(ItemEvent e)
    {
        editor.setItem(e.getItem().toString());
        combo_box.setSelectedItem(e.getItem());
    }

}